package com.hns.can.service.service;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.service.vo.DriverCommentParamsVo;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.tool.pub.PubUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 驾驶员考核各功能通用方法Service
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Service
public class DriverExamService {
    @Autowired
    DriverCommentService driverCommentService;
    /**
     * 校验参数
     * @param vo
     * @author:zhangzq
     * @email:
     */
    private void verifyParams(CommonParamsVo vo){
        if(PubUtil.isEmpty(vo.getOrgIds())){
            throw new BusinessException(MsgType.WARN.getFlag(), "请选择至少一条线路!");
        }
        if(PubUtil.isEmpty(vo.getBeginTime())){
            throw new BusinessException(MsgType.WARN.getFlag(), "请选择时间时间！");
        }
    }

    /**
     * 校验参数
     * @param vo
     * @param user
     * @param obj
     * @author:zhangzq
     * @email:
     */
    public void initParams(CommonParamsVo vo,SysUserEntity user,Object obj){
        verifyParams(vo);
        vo.setCorpId(user.getCorpId());
        vo.setUserid(user.getId());
        if(obj instanceof String){
            String tabName = (String)obj;
            vo.setDriverRelTableName(tabName);
        }else if(obj instanceof List){
            List<String> tabNameList =  (List<String>)obj;
            vo.setDriverRelTableListName(tabNameList);
        }
    }

   /**
    * 日期根据不同设置时间参数
    * @param date
    * @param dateType
    * @param params
    * @author:zhangzq
    * @email:
    */
    public void getReportDate(String date, String dateType, CommonParamsVo params){
        //生成日历对象
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        if(dateType.equals("1")){//选择日
            Date strtodate = formatter.parse(date, pos);
            c.setTime (strtodate);
            params.setReportDate(date);
            params.setReportType("DATE0001");
        }else if(dateType.equals("2")){//选择周
            params.setReportDate(date.substring(0, 7)+"W");//+c.get(Calendar.WEEK_OF_YEAR)+"w");
            params.setReportType("DATE0002");
        }else if(dateType.equals("3")){//选择月
            params.setReportDate(date.substring(0, 7));
            params.setReportType("DATE0003");
        }else if(dateType.equals("4")){//季度
            dateType = date.substring(0, 7) + "Q";
            params.setReportDate(dateType);
            params.setReportType("DATE0004");
        }else if(dateType.equals("5")){//年
            params.setReportDate(date.substring(0, 4));
            params.setReportType("DATE0006");
        }
    }

    /**
     * 获取评语公共方法
     * @param vo
     * @param user
     * @param methodNames
     * @author:zhangzq
     * @email:
     */
    public List<String> getComments(DriverCommentParamsVo vo,SysUserEntity user, String[] methodNames) throws Exception{
        vo.setCorpId(user.getCorpId());
        Class<DriverCommentService> clazz = DriverCommentService.class;
        List<String> list = new ArrayList<String>();
        for(String methodName : methodNames){
            Method method = clazz.getDeclaredMethod(methodName, DriverCommentParamsVo.class);
            String comments = (String)method.invoke(driverCommentService, vo);
            list.add(comments);
        }
        return list;
    }
}